import { ref } from "vue";
import { Cardmin, BIndType } from "../components/card/script/card";
import { fans_alert } from "../components/fans_alert/script/fans_alert";
import { get } from "@/comm/request";
import { URL_LOGIN_TWITTER, URL_TG_TOKEN, URL_TWITTER_CALLBACK } from "@/comm/api";
import { useUser } from "@/stores/user";
import openLoding from "@/comm/openLoding";
import { toast } from "@/components/toast/toast";
import { getDataS } from "../../script/reward";
import { energy } from "../../energy_checkIn/script/energy";
import { getData as getdata } from "../../gotten/script/gotten";
import { getData } from "../../baner/components/script/nameList";

const user = useUser();
let timer: any
let oldTime: number
const check_fans = {
    icons: ref([new Cardmin(BIndType.TG, false, 99999,false), new Cardmin(BIndType.TWITTER, false, 0,false)]),
    async callfun(item: Cardmin) {
        console.log("item ----", item);
        if (item.label == "Telegram") {
            let temp : any = new Date().getTime()
            localStorage.setItem('oldTgTime', String(new Date(temp).getDate()))
            if (user?.info?.telegramBound === 1) {
                return;
            }
            if (!user.info) {
                window.open(`https://t.me/Chainingview`);
                return;
            }
            this.isLunXun()
            let res: any = await get(URL_TG_TOKEN)
            console.log(res)
            window.open(`https://telegram.me/Chainingview_bot?start=${res.data}`);
            // await user.getJoinNumberReword(true);
            this.init();
            // check_fans.icons.value[0].active = true;
        }
        else {
            let temp : any = new Date().getTime()
            localStorage.setItem('oldTwTime', String(new Date(temp).getDate()))
            if (!user.info) {
                window.open("https://twitter.com/intent/follow?screen_name=ChainingView");
                return;
            }
            if (user.info.twitterBound === 1) {
                return;
            }
            // window.open("https://twitter.com/intent/follow?screen_name=ChainingView");
            // await user.getJoinNumberReword(false);
            this.init();
            // check_fans.icons.value[1].active = true;
            // return;
            this.isLunXun()
            const windowWidth = window.innerWidth;
            const windowHeight = window.innerHeight;
            const w = 600;
            const h = 500;
            const left = (windowWidth - w) / 2;
            const top = (windowHeight - h) / 2;
            if (user.isLogin) {
                console.log("yzt", item);
                fans_alert.changeType(item.label);
                if (fans_alert.bindType.value == BIndType.TWITTER) {
                    localStorage.setItem("join", "1");
                    get(URL_LOGIN_TWITTER).then((res: any) => {
                        console.log("URL_LOGIN_TWITTER", res);
                        if (res.code == 200) {
                            // login(res.data);
                            window.open(res.data, "newWindow", `width=${w},height=${h},left=${left},top=${top}`);
                            // window.open('https://twitter.com/intent/user?screen_name=ChainingView', '_blank');
                        } else {
                            toast(res.info);
                        }
                    });
                } else if (fans_alert.bindType.value == BIndType.TG) {
                }
                // fans_alert.open();
            } else {
                openLoding();
            }
        }
        this.init();
    },
    async init(flag = false) {
        let temp : any = new Date().getTime()
        this.icons.value[0].isTap =localStorage.getItem('oldTgTime')!=String(new Date(temp).getDate());
        this.icons.value[1].isTap = localStorage.getItem('oldTwTime')!=String(new Date(temp).getDate());
        if (user.info) {
       
            // this.icons.value[0].active = user.info.telegramBound == 1;
            // this.icons.value[1].active = user.info.twitterBound == 1;
            this.icons.value[0].active = !!user.energyCount.telegramEnergy;
            this.icons.value[1].active = !!user.energyCount.twitterEnergy;
            this.icons.value[0].reward = user.energyCount.telegramEnergy;
            this.icons.value[1].reward = user.energyCount.twitterEnergy;

            this.icons.value[0].computesub()
            this.icons.value[1].computesub()
            // getDataS()
            if (flag) {
                getdata();//能量列表请求
                getData()
                energy.getuserEnergyCount();//能量列表
            }

        }
    },
    isLunXun() {
        oldTime = new Date().getTime()
        setTimeout(() => {
            if (user.info) {
                if (user.info.telegramBound == 0 || user.info.twitterBound == 0) {
                    this.pollingUser()
                }
            }
        }, 2400);
    },
    // 轮询查询用户tg状态
    pollingUser() {
        if (timer) {
            clearInterval(timer)
        }
        timer = setInterval(async () => {
            console.log("触发定时器了")
            // await user.getuserEnergyCount()
            user.getJoinNumberReword()
            this.init(true)
            // if (user.energyCount.telegramEnergy || user.energyCount.twitterEnergy) {
            //     user.getJoinNumberReword()
            // }
            if (user.energyCount.telegramEnergy && user.energyCount.twitterEnergy) {
                if (timer) {
                    clearInterval(timer)
                }

            }
            let currTime = new Date().getTime()
            console.log(currTime - oldTime);
            if ((currTime - oldTime) > 5 * 60 * 1000) {
                clearInterval(timer)
            }
        }, 3000)
    }
};
export { check_fans, timer };
